﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;

namespace HospitalSystem
{
    public class CDAreceiver
    {
        public static string getPlanNO { get; set; }

        public static bool receiveProfileXML(XmlDocument doc)
        {
            DataClasses1DataContext database = new DataClasses1DataContext();
            bool output = true;
            CDAparser parser = new CDAparser(doc);
            Person person = new Person();
            Employee staff = new Employee();
            Host host = new Host();

            staff.Staff_id = parser.getCDAauthorAssignedAuthorIdExtension();

            staff.Firstname = parser.getCDAauthorAssignedAuthorAssignedPersonNameGiven();
            staff.Lastname =  parser.getCDAauthorAssignedAuthorAssignedPersonNameFamily();
            staff.HostID = parser.getCDAauthorAssignedAuthorHostIDExtension();
            database.Employees.InsertOnSubmit(staff);
            try
            {
                database.SubmitChanges();
            }
            catch { database = new DataClasses1DataContext(); }

            person.Host = parser.getCDAcustodianAssignedCustodianAssignedCustodianRepresentedCustodianOrganizationIdExtension();
            //host.HostName = parser.getCDAcustodianAssignedCustodianRepresentedCustodianOrganizationName();

            //parser.getCDAlegalAuthenticatorAssignedEntityIdExtension();
            //parser.getCDAlegalAuthenticatorAssignedEntityAssignedPersonNameGiven();
            //parser.getCDAlegalAuthenticatorAssignedEntityAssignedPersonNameFamily();
            //parser.getCDAlegalAuthenticatorAssignedEntityHostIDExtension();

            //parser.getCDAauthenticatorTimeValue(time);
            //parser.getCDAauthenticatorAssignedEntityIdExtension();
            //parser.getCDAauthenticatorAssignedEntityIdRoot();
            //parser.getCDAauthenticatorAssignedEntityAssignedPersonNameGiven();
            //parser.getCDAauthenticatorAssignedEntityAssignedPersonNameFamily();
            //parser.getCDAauthenticatorAssignedEntityHostIDExtension();
            ///////////////////////////////////////////////////////////////////////////////

            List<CDAEntryOfProfile> profileList = parser.getEntryOfProfile("CA001");
            Person newPerson;
            foreach (CDAEntryOfProfile entry in profileList)
            {
                newPerson = new Person();
                newPerson.RecordedStaffID = parser.getCDAauthorAssignedAuthorIdExtension();
                newPerson.DateTimeUpdate = DateTime.ParseExact(parser.getCDAauthenticatorTimeValue(), "yyyyMMdd", CultureInfo.InvariantCulture);
                newPerson.CID = "" + entry.recordTargetpPatientRoleIdExtension;
                newPerson.LiveHomeStatus = "" + entry.recordTargetpPatientRoleAddrLiveHomeStatus;
                newPerson.LiveHouseNumber = "" + entry.recordTargetpPatientRoleAddrLiveHouseNumber;
                newPerson.LiveMooNumber = "" + entry.recordTargetpPatientRoleAddrLiveMooNumber;
                newPerson.LiveVillageName = "" + entry.recordTargetpPatientRoleAddrLiveVillageName;
                newPerson.LiveAlley = "" + entry.recordTargetpPatientRoleAddrLiveAlley;
                newPerson.LiveStreetName = "" + entry.recordTargetpPatientRoleAddrLiveStreetName;
                newPerson.LiveTumbon = "" + entry.recordTargetpPatientRoleAddrLiveTumbon;
                newPerson.LiveCity = "" + entry.recordTargetpPatientRoleAddrLiveCity;
                newPerson.LiveProvince = "" + entry.recordTargetpPatientRoleAddrLiveProvince;
                newPerson.LivePostCode = "" + entry.recordTargetpPatientRoleAddrLivePostCode;
                newPerson.CensusHouseNumber = "" + entry.recordTargetpPatientRoleAddrCensusHouseNumber;
                newPerson.CensusMooNumber = "" + entry.recordTargetpPatientRoleAddrCensusMooNumber;
                newPerson.CensusVillageName = "" + entry.recordTargetpPatientRoleAddrCensusVillageName;
                newPerson.CensusAlley = "" + entry.recordTargetpPatientRoleAddrCensusAlley;
                newPerson.CensusStreetName = "" + entry.recordTargetpPatientRoleAddrCensusStreetName;
                newPerson.CensusTumbon = "" + entry.recordTargetpPatientRoleAddrCensusTumbon;
                newPerson.CensusCity = "" + entry.recordTargetpPatientRoleAddrCensusCity;
                newPerson.CensusProvince = "" + entry.recordTargetpPatientRoleAddrCensusProvince;
                newPerson.CensusMoveInDate = DateTime.ParseExact(entry.recordTargetpPatientRoleAddrCensusMoveInDate, "yyyyMMdd", CultureInfo.InvariantCulture);
                if (entry.recordTargetpPatientRoleAddrCensusMoveOutDate != "")
                {
                    newPerson.CensusMoveOutDate = DateTime.ParseExact(entry.recordTargetpPatientRoleAddrCensusMoveOutDate, "yyyyMMdd", CultureInfo.InvariantCulture);
                }
                newPerson.CensusMoveOutReason = "" + entry.recordTargetpPatientRoleAddrCensusMoveOutReason;

                newPerson.HomePhone = "" + entry.recordTargetpPatientRoleHomePhoneValue;
                newPerson.Mobile = "" + entry.recordTargetpPatientRoleMobileValue;
                newPerson.Title = "" + entry.recordTargetpPatientRolePatientNameTitle;
                newPerson.FirstName = "" + entry.recordTargetpPatientRolePatientNameGiven;
                newPerson.LastName = "" + entry.recordTargetpPatientRolePatientNameFamily;

                newPerson.Gender = "" + entry.recordTargetpPatientRolePatientAdministrativeGenderCodeCode;
                newPerson.DOB = DateTime.ParseExact(entry.recordTargetpPatientRolePatientBirthTimeValue, "yyyyMMdd", CultureInfo.InvariantCulture);
                //entry.recordTargetpPatientRolePatientSubWelfareIDValue = "" + "04";
                newPerson.BloodType = "" + entry.recordTargetpPatientRolePatientBloodTypeValue;
                newPerson.Nation = "" + entry.recordTargetpPatientRolePatientNationValue;
                newPerson.Race = "" + entry.recordTargetpPatientRolePatientRaceValue;
                newPerson.Religion = "" + entry.recordTargetpPatientRolePatientReligionValue;
                newPerson.Foreigner = "" + entry.recordTargetpPatientRolePatientForeignerValue;
                //entry.recordTargetpPatientRolePatientChildPicValue = "" + newPerson.Child_Pic;
                //entry.recordTargetpPatientRolePatientChildPicCIDValue = "" + newPerson.Child_PicCID;

                database.Persons.InsertOnSubmit(newPerson);
                try
                {
                    database.SubmitChanges();
                }
                catch
                {
                    database = new DataClasses1DataContext();
                    output = false;
                }

            }

            return output;
        }

        public static bool receiveCasemanagerPlanningsXML(XmlDocument doc)
        {
            DataClasses1DataContext database = new DataClasses1DataContext();
            bool output = true;
            CDAparser parser = new CDAparser(doc);
            DateTime time = DateTime.ParseExact(parser.getCDAeffectiveTime(), "yyyyMMdd", CultureInfo.InvariantCulture);
            Person person = new Person();

            person.CID = parser.getCDArecordTargetPatientRoleIdExtension();
            person.FirstName = parser.getCDArecordTargetPatientRolePatientNameGiven();
            person.LastName = parser.getCDArecordTargetPatientRolePatientNameFamily();
            person.Gender = parser.getCDArecordTargetPatientRolePatientAdministrativeGenderCode();

            person.DOB = DateTime.ParseExact(parser.getCDArecordTargetPatientRolePatientBirthTime(), "yyyyMMdd", CultureInfo.InvariantCulture);

            //parser.getCDAauthorAssignedAuthorIdExtension(staff.StaffID);

            //parser.getCDAauthorAssignedAuthorAssignedPersonNameGiven(staffName.Firstname);
            //parser.getCDAauthorAssignedAuthorAssignedPersonNameFamily(staffName.Lastname);
            //parser.getCDAauthorAssignedAuthorHostIDExtension(); รหัส ศุนย์การศึกษา

            //person = parser.getCDAcustodianAssignedCustodianAssignedCustodianRepresentedCustodianOrganizationIdExtension();
            //parser.getCDAcustodianAssignedCustodianRepresentedCustodianOrganizationName();

            person.DateTimeUpdate = DateTime.ParseExact(parser.getCDAlegalAuthenticatorTimeValue(), "yyyyMMdd", CultureInfo.InvariantCulture);
            //parser.getCDAlegalAuthenticatorAssignedEntityIdExtension();
            //parser.getCDAlegalAuthenticatorAssignedEntityAssignedPersonNameGiven();
            //parser.getCDAlegalAuthenticatorAssignedEntityAssignedPersonNameFamily();
            //parser.getCDAlegalAuthenticatorAssignedEntityHostIDExtension();

            //parser.getCDAauthenticatorTimeValue(time);
            //parser.getCDAauthenticatorAssignedEntityIdExtension();
            //parser.getCDAauthenticatorAssignedEntityIdRoot();
            //parser.getCDAauthenticatorAssignedEntityAssignedPersonNameGiven();
            //parser.getCDAauthenticatorAssignedEntityAssignedPersonNameFamily();
            //parser.getCDAauthenticatorAssignedEntityHostIDExtension();
            ///////////////////////////////////////////////////////////////////////////////

            try
            {
                database.Persons.InsertOnSubmit(person);
                database.SubmitChanges();
            }
            catch
            {
                database = new DataClasses1DataContext();
                output = false;
            }

            string planNo = parser.getCDAcomponentPlanNumber();
            getPlanNO = planNo;
            string caseNo = parser.getCDAcomponentCaseNumber();
            List<CDAEntry> entryList = parser.getEntryList("CA003");
            #region
            CMPlan rec;
            foreach (CDAEntry entry in entryList)
            {
                rec = new CMPlan();
                rec.CID = parser.getCDArecordTargetPatientRoleIdExtension();
                rec.CMPlanNo = planNo;
                rec.PlanActCode = entry.observationCode;
                if (entry.observationEffectiveTimeCenterValue.Length > 0)
                {
                    rec.PlanTime = DateTime.ParseExact(entry.observationEffectiveTimeCenterValue, "yyyyMMdd", CultureInfo.InvariantCulture);
                }
                try
                {
                    database.CMPlans.InsertOnSubmit(rec);
                    database.SubmitChanges();
                }
                catch
                {
                    database = new DataClasses1DataContext();
                    output = false;
                }
            }
            #endregion

            return output;
        }

        public static bool receiveCasemanagerQuestionareXML(XmlDocument doc)
        {
            DataClasses1DataContext database = new DataClasses1DataContext();
            bool output = true;
            CDAparser parser = new CDAparser(doc);
            DateTime time = DateTime.ParseExact(parser.getCDAeffectiveTime(), "yyyyMMdd", CultureInfo.InvariantCulture);
            Person person = new Person();

            person.CID = parser.getCDArecordTargetPatientRoleIdExtension();
            person.FirstName = parser.getCDArecordTargetPatientRolePatientNameGiven();
            person.LastName = parser.getCDArecordTargetPatientRolePatientNameFamily();
            person.Gender = parser.getCDArecordTargetPatientRolePatientAdministrativeGenderCode();

            person.DOB = DateTime.ParseExact(parser.getCDArecordTargetPatientRolePatientBirthTime(), "yyyyMMdd", CultureInfo.InvariantCulture);

            //parser.getCDAauthorAssignedAuthorIdExtension(staff.StaffID);

            //parser.getCDAauthorAssignedAuthorAssignedPersonNameGiven(staffName.Firstname);
            //parser.getCDAauthorAssignedAuthorAssignedPersonNameFamily(staffName.Lastname);
            //parser.getCDAauthorAssignedAuthorHostIDExtension(); รหัส ศุนย์การศึกษา

            //person = parser.getCDAcustodianAssignedCustodianAssignedCustodianRepresentedCustodianOrganizationIdExtension();
            //parser.getCDAcustodianAssignedCustodianRepresentedCustodianOrganizationName();

            person.DateTimeUpdate = DateTime.ParseExact(parser.getCDAlegalAuthenticatorTimeValue(), "yyyyMMdd", CultureInfo.InvariantCulture);
            //parser.getCDAlegalAuthenticatorAssignedEntityIdExtension();
            //parser.getCDAlegalAuthenticatorAssignedEntityAssignedPersonNameGiven();
            //parser.getCDAlegalAuthenticatorAssignedEntityAssignedPersonNameFamily();
            //parser.getCDAlegalAuthenticatorAssignedEntityHostIDExtension();

            //parser.getCDAauthenticatorTimeValue(time);
            //parser.getCDAauthenticatorAssignedEntityIdExtension();
            //parser.getCDAauthenticatorAssignedEntityIdRoot();
            //parser.getCDAauthenticatorAssignedEntityAssignedPersonNameGiven();
            //parser.getCDAauthenticatorAssignedEntityAssignedPersonNameFamily();
            //parser.getCDAauthenticatorAssignedEntityHostIDExtension();
            ///////////////////////////////////////////////////////////////////////////////

            try
            {
                database.Persons.InsertOnSubmit(person);
                database.SubmitChanges();
            }
            catch
            {
                database = new DataClasses1DataContext();
                output = false;
            }

            string planNo = parser.getCDAcomponentPlanNumber();
            getPlanNO = planNo.Trim();
            string caseNo = parser.getCDAcomponentCaseNumber();
            List<CDAEntry> entryList = parser.getEntryList("CA002");
            #region
            CMQuestionnaire questH;
            foreach (CDAEntry entry in entryList)
            {
                questH = new CMQuestionnaire();
                questH.CID = parser.getCDArecordTargetPatientRoleIdExtension();
                questH.CMPlanNo = getPlanNO;
                questH.Qvalue = entry.observationValueCode;
                questH.QCode = entry.observationCode;
                if (entry.observationEffectiveTimeCenterValue.Length > 0)
                {
                    questH.Qtime = DateTime.ParseExact(entry.observationEffectiveTimeCenterValue, "yyyyMMdd", CultureInfo.InvariantCulture);
                }
                try
                {
                    database.CMQuestionnaires.InsertOnSubmit(questH);
                    database.SubmitChanges();
                }
                catch
                {
                    database = new DataClasses1DataContext();
                    output = false;
                }
            }
            #endregion

            return output;
        }
    }
}